package net.zjitc.controller.admin;

import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;

/**
 * @Author admin
 * @DATE 2019/3/28 13:16
 */
@Controller
@RequestMapping("/manage")
public class AdminController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session, Model model){
        User user = userService.login(username, password);
        if (user!= null){
            if (!"超级用户".equals(user.getRole())){
                model.addAttribute("msg","你不是管理员，没有权限登录！");
                return "/admin/login/login.jsp";
            }else {
                session.setAttribute("admin",user);
                return "/admin/login/home.jsp";
            }
        }
        model.addAttribute("msg","用户名或密码错误");

        return  "/admin/login/login.jsp";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("admin");
        return "redirect:/manage/index.do";
    }

    @RequestMapping("/index")
    public String loginUI(HttpSession session){
        return "/admin/login/login.jsp";
    }
}
